<?php

use think\migration\Seeder;

class InitSheep extends Seeder
{
    public function run()
    {
        $timestamps = [
            'create_time' => time(),
            'update_time' => time(),
        ];

        // ===================== 添加超级角色 =====================
        $adminRole = $this->table('admin_role');

        $result = $adminRole->getAdapter()->fetchRow("select count(*) as has from " . $adminRole->getAdapter()->getAdapterTableName('admin_role') . " where rules = '*'");
        if ($result['has']) {
            // 已经存在超级角色，则提示，并跳过
            $this->output->writeln(' ==' . ' <info>' . get_class($this) . ':</info>' . ' <comment>The super admin role already exists.</comment>');
        } else {
            $data = array_merge([
                'parent_id' => 0,
                'name' => '超级管理组',
                'code' => 'root',
                'rules' => '*',
                'status' => 'normal',
            ], $timestamps);

            // 初始化超级管理组
            $adminRole->insert($data)->save();

            $this->output->writeln(' ==' . ' <info>' . get_class($this) . ':</info>' . ' <comment>The admin role create successed.</comment>');
        }


        // ===================== 配置初始化 =====================
        $config = $this->table('config');

        $codes = $config->getAdapter()->fetchAll("select code from " . $config->getAdapter()->getAdapterTableName('config'));
        $codes = array_column($codes, 'code');

        $initConfigFile = root_path() . 'database' . DIRECTORY_SEPARATOR . 'seeds' . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'InitConfig.php';
        $datas = include $initConfigFile;

        foreach ($datas as $key => $data) {
            if (!in_array($data['code'], $codes)) {
                $data['value'] = is_array($data['value']) ? json_encode($data['value']) : $data['value'];
                $data['store_range'] = is_array($data['store_range']) ? json_encode($data['store_range']) : $data['store_range'];
                $config->insert($data)->save();
            }
        }
        $this->output->writeln(' ==' . ' <info>' . get_class($this) . ':</info>' . ' <comment>The config create successed.</comment>');

        // ===================== 权限菜单初始化 =====================
        $access = $this->table('access');

        $names = $access->getAdapter()->fetchAll("select name from " . $access->getAdapter()->getAdapterTableName('access'));
        $names = array_column($names, 'name');

        $initAccessFile = root_path() . 'database' . DIRECTORY_SEPARATOR . 'seeds' . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'InitAccess.php';
        $accesses = include $initAccessFile;

        foreach ($accesses as $key => $accessData) {
            if (!in_array($accessData['name'], $names)) {
                $access->insert($accessData)->save();
            }
        }
        $this->output->writeln(' ==' . ' <info>' . get_class($this) . ':</info>' . ' <comment>The access create successed.</comment>');

        // ===================== 文件分组表 =====================
        $groupData = array_merge([
            'name' => '默认分组',
            'group' => 'default'
        ], $timestamps);
        $fileGroup = $this->table('file_group');
        $result = $fileGroup->getAdapter()->fetchRow("select count(*) as has from " . $fileGroup->getAdapter()->getAdapterTableName('file_group') . " where `group` = 'default'");
        if ($result['has']) {
            // 已经存在超级角色，则提示，并跳过
            $this->output->writeln(' ==' . ' <info>' . get_class($this) . ':</info>' . ' <comment>The default file group already exists.</comment>');
        } else {
            $fileGroup->insert($groupData)->save();
            $this->output->writeln(' ==' . ' <info>' . get_class($this) . ':</info>' . ' <comment>The default file group create successed.</comment>');
        }
    }
}
